﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using FU.Capstones.IMS.Web.Common;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.DataContracts;
using FU.Capstones.IMS.Web.Common.InventoryServiceProxy.EventArgs;
using FU.Capstones.IMS.Web.Silverlight.Models;
using Microsoft.Practices.Prism.Commands;

namespace FU.Capstones.IMS.Web.Silverlight.Popups
{
    public partial class AddListPopup : INotifyPropertyChanged
    {
        public AddListPopup(int productID)
        {
            InitializeComponent();

            DataContext = this;
            Model.GetMaterialCollectionTypeCompleted += DoGetMaterialCollectionTypeCompleted;

            ServiceCallCounter += Model.GetMaterialCollectionTypeAsync(false);
            
        //    model.InsertContactCompleted += AddCompleted;
        }

        AddListModel Model = new AddListModel();

        #region DoCompleted

        void DoGetMaterialCollectionTypeCompleted(object sender, CollectionFeedbackCompletedEventArgs<Material> e)
        {
            ServiceCallCounter -= WebConstants.MagicNumer;
            if (e.Error != null)
            {
                Message = e.Error.Message;
                MessageVisibility = Visibility.Collapsed;
            }
            else
            {
                //Message = e.Result.Message;
                if (!e.Result.IsSuccessful)
                {
                    MessageVisibility = Visibility.Visible;
                }
                else
                {
                    MessageVisibility = Visibility.Collapsed;
                    MaterialCollection = new PagedCollectionView(e.Result.Data);
                    MappWithCollection();
                }
            }
        }

        #endregion

        #region Command

        #region SelectAllCommand

        private DelegateCommand<object> selectAllCommand;
        public DelegateCommand<object> SelectAllCommand
        {
            get
            {
                if (selectAllCommand == null)
                    selectAllCommand = new DelegateCommand<object>(DoSelectAllCommand);
                return selectAllCommand;
            }
        }

        private void DoSelectAllCommand(object obj)
        {
            foreach (var material1 in MaterialCollection.Cast<Material>())
            {
                material1.IsSelected = true;
            }
        }

        #endregion

        #region UnselectAllCommand

        private DelegateCommand<object> unselectAllCommand;
        public DelegateCommand<object> UnselectAllCommand
        {
            get
            {
                if (unselectAllCommand == null)
                    unselectAllCommand = new DelegateCommand<object>(DoUnselectAllCommand);
                return unselectAllCommand;
            }
        }

        private void DoUnselectAllCommand(object obj)
        {
            foreach (var material in MaterialCollection.Cast<Material>())
            {
                material.IsSelected = false;
            }
        }

        #endregion

        #region SaveCommand

        private DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                if (saveCommand == null)
                    saveCommand = new DelegateCommand<object>(DoSaveCommand);
                return saveCommand;
            }
        }

        private void DoSaveCommand(object obj)
        {
            DialogResult = true;
        }

        #endregion

        #region CancelCommand

        private DelegateCommand<object> cancelCommand;
        public DelegateCommand<object> CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                    cancelCommand = new DelegateCommand<object>(DoCancelCommand);
                return cancelCommand;
            }
        }

        private void DoCancelCommand(object obj)
        {
            DialogResult = false;
        }

        #endregion

        #endregion

        #region Binding Property

        private PagedCollectionView materialCollection;
        public PagedCollectionView MaterialCollection
        {
            get { return materialCollection; }
            set
            {
                if (!ReferenceEquals(materialCollection, value))
                {
                    materialCollection = value;
                    RaisePropertyChanged("MaterialCollection");
                }
            }
        }

        private ObservableCollection<Material> selectedMaterialCollection;
        public ObservableCollection<Material> SelectedMaterialCollection
        {
            get { return selectedMaterialCollection; }
            set
            {
                if (selectedMaterialCollection != value)
                {
                    selectedMaterialCollection = value;
                    RaisePropertyChanged("SelectedMaterialCollection");
                }
            }
        }

     
        private Visibility messageVisibility;
        public Visibility MessageVisibility
        {
            get { return messageVisibility; }
            set
            {
                if (messageVisibility != value)
                {
                    messageVisibility = value;
                    RaisePropertyChanged("MessageVisibility");
                }
            }
        }

        private string message;
        public string Message
        {
            get { return message; }
            set
            {
                if (message != value)
                {
                    message = value;
                    RaisePropertyChanged("Message");
                }
            }
        }

        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                if (isBusy != value)
                {
                    isBusy = value;
                    RaisePropertyChanged("IsBusy");
                }
            }
        }

        private int serviceCallCounter;
        public int ServiceCallCounter
        {
            get { return serviceCallCounter; }
            set
            {
                if (serviceCallCounter != value)
                {
                    serviceCallCounter = value;
                    RaisePropertyChanged("ServiceCallCounter");
                    //if (serviceCallCounter == 0)
                    //{
                    //    HandleLoadingCompleted();
                    //}
                }
            }
        }

        #endregion

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private void MappWithCollection()
        {
            if (SelectedMaterialCollection != null && MaterialCollection != null)
            {
                int count = SelectedMaterialCollection.Count;
                for (int i = 0; i < count; i++)
                {
                    var material = SelectedMaterialCollection[i];
                    material = MaterialCollection.SourceCollection.Cast<Material>().FirstOrDefault(x =>
                                                            {
                                                                if (x.MaterialID == material.MaterialID)
                                                                {
                                                                    x.Quantity = material.Quantity;
                                                                    x.IsSelected = true;
                                                                    return true;
                                                                }
                                                                return false;
                                                            });
                }
            }
        }
        
    }
}

